{extend name="app/admin/view/base.html"/}

{block name="resources"}
<style type="text/css">
.js-domain{
	display: none;
}
.refresh-time {
	color: #B2B2B2;
	margin-top: 10px;
}
</style>
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>为满足不同用户的需求，方便快速搭建Web端，增加以下三种部署方式供其选择，易上手难度递增。</li>
			<li>（难度：简单）默认部署：无需下载，一键刷新，API接口请求地址默认为当前域名，编译代码存放到web文件夹中。</li>
			<li>（难度：中等）独立部署：下载编译代码包，参考开发文档进行配置。</li>
			<li>（难度：较高）独立部署：下载开源代码包，参考开发文档进行配置，结合业务需求进行二次开发。</li>
			<li>开发文档参考：<a href="https://docs.qq.com/doc/DZkpOU2h5b2NFUWtG" target="_blank" class="ns-text-color">NiuShop多商户PC端开发文档</a></li>
		</ul>
	</div>
</div>
<div class="layui-form ns-form">

	<div class="layui-form-item">
		<label class="layui-form-label">部署方式：</label>
		<div class="layui-input-block">
			<input type="radio" name="deploy_way" value="default" lay-filter="deploy_way" title="默认部署" data-desc="无需下载，一键刷新，API接口请求地址为当前域名，编译代码存放到web文件夹中" {if $config.deploy_way eq 'default'}checked{/if} />
			<input type="radio" name="deploy_way" value="indep" lay-filter="deploy_way" title="独立部署" data-desc="下载编译包或源码包进行独立部署" {if $config.deploy_way eq 'indep'}checked{/if}/>
		</div>
		<div class="ns-word-aux js-desc">
			{if $config.deploy_way eq 'default'}
				无需下载，一键刷新，API接口请求地址为当前域名，编译代码存放到web文件夹中
			{else/}
				下载编译包或源码包进行独立部署
			{/if}
		</div>
	</div>

	<div class="deploy-way default {if $config.deploy_way neq 'default'}layui-hide{/if}">
        <div class="layui-form-item web-url">
            <label class="layui-form-label">电脑端域名：</label>
            <div class="layui-input-block">
                <a href="{$root_url}/web" target="_blank" class="ns-text-color">{$root_url}/web</a>
            </div>
        </div>
        <div class="ns-form-row" style="margin-top: 0;">
            <button class="layui-btn ns-bg-color js-save" onclick="refresh()">刷新</button>
            {if $refresh_time}<p class="refresh-time">上次刷新时间：{:time_to_date($refresh_time)}</p>{/if}
        </div>
    </div>
	
    <div class="deploy-way indep {if $config.deploy_way neq 'indep'}layui-hide{/if}">
        <div class="layui-form-item">
            <label class="layui-form-label">电脑端域名：</label>
            <div class="layui-input-block">
                <input type="text" name="domain" lay-verify="domain" {if $config.deploy_way eq 'indep'}value="{$config['domain_name_pc'] ?? ''}"{/if} autocomplete="off" class="layui-input ns-len-long">
            </div>
            <div class="ns-word-aux">域名末尾无需拼接“/”</div>
        </div>
        <div class="layui-form-item web-url">
            <label class="layui-form-label">源码下载：</label>
            <div class="layui-input-block">
                <button class="layui-btn ns-bg-color" lay-submit lay-filter="downloadindep">编译包下载</button>
                <button class="layui-btn ns-bg-color" onclick="window.open(ns.url('pc://admin/pc/downloados'));">源码包下载</button>
            </div>
            <div class="ns-word-aux">编译包下载之后直接解压到电脑端域名根目录即部署完成。</div>
            <div class="ns-word-aux">源码包下载之后可进行二次开发，可自行发行进行部署。</div>
        </div>
    </div>
   
    <div class="ns-form-row">
        <button class="layui-btn ns-bg-color js-save" lay-submit lay-filter="save">保存</button>
    </div>
</div>
{/block}

{block name="script"}
<script type="text/javascript">
	layui.use(['form'], function() {
		var form = layui.form,
			repeat_flag = false; //防重复标识;

		form.on('radio(deploy_way)', function(data){
            var value = $(data.elem).val();
            $('.deploy-way').addClass('layui-hide');
            $('.deploy-way.' + value).removeClass('layui-hide');

            var desc = $(data.elem).attr("data-desc");
            $(".js-desc").text(desc);
        })

		form.verify({
			domain : function(value, item) {
			  	var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/; //正则表达式验证域名
				if($("input[name='deploy_way']:checked").val() == "indep") {
					if (value === '') {
						return "请输入域名地址";
					} else if (!(reg.test(value))) {
						return '请输入正确的域名地址';
					}
				}
			}
		});

		form.on("submit(save)",function(data){
			if (repeat_flag) return false;
            repeat_flag = true;
            $.ajax({
                url: ns.url("pc://admin/pc/pcdomainname"),
				data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function(res){
                    repeat_flag = false;
                    layer.msg(res.message);
                }
            });
		})

		form.on("submit(downloadindep)",function(data){
            window.open(ns.url('pc://admin/pc/downloadcsindep', {domain: data.field.domain}));
        }) 
	});

	function refresh(){
        if (repeat_flag) return false;
        repeat_flag = true;
        $.ajax({
			url: ns.url("pc://admin/pc/downloadcsdefault"),
			dataType: 'JSON',
			type: 'POST',
			success: function(res){
				repeat_flag = false;
				layer.msg(res.message);
			}
		});
    }
</script>
{/block}